package org.example.journal_items.config;

import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket;
import io.github.bucket4j.Bucket4j;
import io.github.bucket4j.Refill;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
public class RateLimitConfig {

  @Bean
  public Bucket createNewBucket() {
    // 每秒生成10个令牌
    Refill refill = Refill.intervally(10, Duration.ofSeconds(1));
    // 桶容量为100个令牌
    Bandwidth limit = Bandwidth.classic(100, refill);
    return Bucket4j.builder().addLimit(limit).build();
  }
}